package com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.wastePart;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.constant.DangerousManagementConstant;
import com.xidian.zhiancloud.dao.WasteWarehouseOutMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dto.ResultDTO;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.WasteInfo;
import com.xidian.zhiancloud.model.WasteType;
import com.xidian.zhiancloud.model.WasteWarehouseOut;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.chemicalsManagement.WasteWarehouseOutQO;
import com.xidian.zhiancloud.service.chemicalsManagement.chemicalManager.CodeService;
import com.xidian.zhiancloud.util.zyf.ExcelUtils;
import com.xidian.zhiancloud.vo.chemicalsManagement.WasteWarehouseOutVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Author: Egoist
 * @Date: 2020/11/10 17:00
 */

@Service
public class WasteWarehouseOutService extends ServiceImpl<WasteWarehouseOutMapper, WasteWarehouseOut> implements IService<WasteWarehouseOut> {

    @Autowired
    private WasteWarehouseOutMapper wasteWarehouseOutMapper;
    @Autowired
    private WasteInfoService wasteInfoService;
    @Autowired
    private WasteTypeService wasteTypeService;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private WasteStorageService wasteStorageService;
    @Autowired
    private CodeService codeService;

    public IPage<WasteWarehouseOutVO> getListByQueryObject(WasteWarehouseOutQO wasteWarehouseOutQO) {
        Page<WasteWarehouseOutVO> page = new Page<>(wasteWarehouseOutQO.getCurrent(), wasteWarehouseOutQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("out_time").setAsc(false);
        page.getOrders().add(orderItem);
        IPage<WasteWarehouseOutVO> iPage = wasteWarehouseOutMapper.getListByQO(page, wasteWarehouseOutQO);
        List<WasteWarehouseOutVO> records = iPage.getRecords();
        List<WasteWarehouseOutVO> ans = new ArrayList<>();
        records.forEach(t -> {
            t.setStrQuantity(t.getOutQuantity().toString() + DangerousManagementConstant.QuantityUnit.get(t.getUnit()));
            ans.add(t);
        });
        iPage.setRecords(ans);
        return iPage;
    }

    public WasteWarehouseOutVO getVOById(Long id) {
        WasteWarehouseOut wasteWarehouseOut = getById(id);
        if (wasteWarehouseOut == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        WasteWarehouseOutVO wasteWarehouseOutVO = new WasteWarehouseOutVO();
        BeanUtils.copyProperties(wasteWarehouseOut, wasteWarehouseOutVO);
        Long outerId = wasteWarehouseOut.getOuterId();
        Long wasteId = wasteWarehouseOut.getWasteId();
        WasteInfo wasteInfo = wasteInfoService.getById(wasteId);

        Long typeId = wasteInfo.getTypeId();

        SysUser sysUser = sysUserMapper.selectById(outerId);
        WasteType wasteType = wasteTypeService.getById(typeId);

        wasteWarehouseOutVO.setOuterName(sysUser == null ? "该用户已删除" : sysUser.getName());
        wasteWarehouseOutVO.setWasteName(wasteInfo.getWasteName());
        wasteWarehouseOutVO.setWasteType(wasteType == null ? "该类型已删除" : wasteType.getWasteType());

        return wasteWarehouseOutVO;
    }

    public ResultDTO<Boolean> add(WasteWarehouseOut wasteWarehouseOut) {
        Long wasteId = wasteWarehouseOut.getWasteId();
        WasteInfo wasteInfo = wasteInfoService.getById(wasteId);
        BigDecimal storageQuantity = wasteInfo.getRealQuantity();
        BigDecimal outQuantity = wasteWarehouseOut.getOutQuantity();
        //如果库存过小则返回false
        if (outQuantity.compareTo(storageQuantity) == 1) {
            return ResultDTO.errorOf(-1, "使用量超出库存!");
        }
        //减少库存记录
        wasteInfo.setRealQuantity(storageQuantity.subtract(outQuantity));
        //设置info状态
        BigDecimal maxQuantity = wasteInfo.getMaxQuantity();
        wasteInfo.setState(maxQuantity.compareTo(wasteInfo.getRealQuantity()) == 1 ? 0 : 1);
        wasteInfoService.updateById(wasteInfo);
        //添加出库编码
        String code = codeService.getWasteOutCode(wasteWarehouseOut.getCompanyId());
        wasteWarehouseOut.setOutCode(code);

        return ResultDTO.successOf(save(wasteWarehouseOut), GeneralResponseEnums.SUCCESS);
    }

    public Boolean export(HttpServletResponse response, Long[] ids) throws IOException {
        List<WasteWarehouseOut> list = listByIds(Arrays.asList(ids));
        List<WasteWarehouseOutVO> ans = new ArrayList<>();
        list.forEach(t -> {
            WasteWarehouseOutVO vo = new WasteWarehouseOutVO();
            BeanUtils.copyProperties(t, vo);
            SysUser outUser = sysUserMapper.selectById(vo.getOuterId());
            WasteInfo info = wasteInfoService.getById(vo.getWasteId());
            WasteType type = wasteTypeService.getById(info.getTypeId());
            vo.setOuterName(outUser == null ? "该用户已删除" : outUser.getName());
            vo.setWasteType(type == null ? "该类型已删除" : type.getWasteType());
            vo.setWasteName(info.getWasteName());
            vo.setStrQuantity(vo.getOutQuantity().toString() + DangerousManagementConstant.QuantityUnit.get(info.getUnit()));
            ans.add(vo);
        });

        ExcelUtils.exportExcel(ans,"废弃物出库台账","废弃物出库",WasteWarehouseOutVO.class,"废弃物出库台账",response);
        return true;
    }
}
